package com.manager.web.system;

import java.io.PrintWriter;
import java.sql.Types;
import java.text.ParseException;

import javax.servlet.http.HttpSession;

import com.manager.base.BaseAction;
import com.manager.pojo.system.SysUser;
import com.manager.util.CONSTANT;
import com.manager.util.DateUtil;
import com.manager.util.Log;
import com.manager.util.CodeUtil;
import com.opensymphony.xwork2.Action;

public class SysLoginAction extends BaseAction {
	/*
	 * 用户登录与注销
	 */
	public String queryLogin(){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String checkcode = request.getParameter("checkcode");
		//获取session中验证码的值
		String kaptcha = (String)request.getSession().getAttribute(CONSTANT.KAPTCHA_SESSION_KEY);
		PrintWriter p = null;
		SysUser user = null;
		
		try{
			p = response.getWriter();
			
			if(!checkcode.equals(kaptcha)){
				p.print("{\"flag\":false,\"msg\":\"您输入的验证码不匹配\"}");
			}else{
				//提取用户信息
				String hql = "FROM SysUser u WHERE u.name = ? AND u.password = ?";
				user = (SysUser)super.baseService.getObject(hql, username, password);
				
				if(user == null){
					p.print("{\"flag\":false,\"msg\":\"用户名与密码不匹配\"}");
				}else{
					//先注销再登录
					HttpSession session = request.getSession();
					session.removeAttribute(CONSTANT.SESSION_KEY);
					session.setAttribute(CONSTANT.SESSION_KEY, user);
					p.print("{\"flag\":true}");
				}
			}
		}catch(Exception e){
			p.print("{\"flag\":false,\"msg\":\"未知错误\"}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * 检查登录是否超时
	 */
	public String querySessionTimeout(){
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			Object obj = request.getSession().getAttribute(CONSTANT.SESSION_KEY);
			
			if(obj == null){//session已超时
				p.print("{\"flag\":true}");
			}else{//session正常
				p.print("{\"flag\":false}");
			}
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	/*
	 * 注销当前用户
	 */
	public String queryLoginOff(){
		PrintWriter p = null;
		
		try{
			p = response.getWriter();
			
			HttpSession session = request.getSession();
			session.removeAttribute(CONSTANT.SESSION_KEY);
			
			p.print("{\"flag\":true}");
		}catch(Exception e){
			p.print("{\"flag\":false}");
			e.printStackTrace();
		}finally{
			p.flush();
			p.close();
		}
		
		return Action.NONE;
	}
	
	public String addTest() throws ParseException{
		String sql = "insert into userinfo (class_id, name, age, datetime_d, datetime_v, datetime_l) values (?, ?, ?, ?, ?, ?)";
		String[] ds = new String[11];
		ds[0] = "2010-08-03 22:50:39";
		ds[1] = "2010-07-01 09:10:11";
		ds[2] = "2010-06-02 16:20:22";
		ds[3] = "2010-05-02 16:20:36";
		ds[4] = "2010-04-01 06:30:49";
		ds[5] = "2010-03-05 05:40:59";
		ds[6] = "2010-02-06 06:50:19";
		ds[7] = "2010-01-07 07:59:29";
		ds[8] = "2010-02-08 08:56:39";
		ds[9] = "2010-03-09 09:10:49";
		ds[10] = "2010-04-10 06:10:59";
		
		for(int j = 0; j < 5; j++){
			Log.info("for:" + j);
			Object[][] params = new Object[100000][6];
			
			for(int i = 0; i < params.length; i++){
				int dd = CodeUtil.getRandomInt(0, 10);
				params[i][0] = CodeUtil.getRandomInt(1, 10);
				params[i][1] = CodeUtil.getRandomStr(3);
				params[i][2] = CodeUtil.getRandomInt(1, 100);
				params[i][3] = ds[dd];
				params[i][4] = ds[dd];
				params[i][5] = DateUtil.getDateTimeLong(ds[dd]);
			}
			
			int[] sqlTypes = {Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.DATE, Types.VARCHAR, Types.BIGINT};
//			super.baseService.executeSqlBatch(sql, params, sqlTypes, 1000);
			super.baseService.executeBatch(sql, params);
		}
		return Action.NONE;
	}
}
